package com.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.async.WebAsyncTask;
import org.springframework.web.servlet.ModelAndView;

import java.util.concurrent.Callable;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/8/18
 * Time: 9:31
 * To change this template use Editor | File and Code Templates.
 */
@Controller
@RequestMapping("/async")
public class AsyncController {


    @RequestMapping(value = "/longtimetask", method = RequestMethod.GET)
    public WebAsyncTask longTimeTask() {
        System.out.println("/longtimetask被调用 thread id is : " + Thread.currentThread().getId());
        Callable<ModelAndView> callable = () -> {
            Thread.sleep(3000); //假设是一些长时间任务：上传文件
            ModelAndView mav = new ModelAndView("longtimetask");
            mav.addObject("result", "执行成功");
            System.out.println("执行成功 thread id is : " + Thread.currentThread().getId());
            return mav;
        };
        return new WebAsyncTask(callable);
    }


}
